package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.EduClass;
import henu.dao.IEduClassDao;
import henu.util.Dhcp;

public class EduClassDaoImpl implements IEduClassDao {
	protected QueryRunner qr ;
	
	public EduClassDaoImpl(QueryRunner qr){
		this.qr = qr;
	}
	@Override
	public int save(EduClass educ) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "insert into tb_educlass(fTeachingClassNumber,fClassNumber,fTeacherId,fStuNumber,fRemark) values(?,?,?,?,?)";
		Object[] param = {educ.getfTeachingClassNumber(),educ.getfClassNumber(),educ.getfTeacherId(),educ.getfStuNumber(),educ.getfRemark()};
		int n = 0;
		try {
			n = qr.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int delete(int fTeachingClassNumber) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "delete from tb_educlass where fTeachingClassNumber=?";
		int n = 0;
		try {
			n = qr.update(sql, fTeachingClassNumber);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int update(int fTeachingClassNumber, EduClass educ) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "update tb_educlass ec set ec.fClassNumber=?,ec.fTeacherId=?,ec.fStuNumber=?,ec.fRemark=? where ec.fTeachingClassNumber=?";
		Object[] param = {educ.getfClassNumber(),educ.getfTeacherId(),educ.getfStuNumber(),educ.getfRemark(),fTeachingClassNumber};
		int n = 0;
		try {
			n = qr.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public EduClass select(int fTeachingClassNumber) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_educlass where fTeachingClassNumber = ?";
		EduClass educ = null;
		try {
			educ = qr.query(sql,fTeachingClassNumber,new BeanHandler<EduClass>(EduClass.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return educ;
	}

	@Override
	public List<EduClass> getAll() {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_educlass";
		List<EduClass> educs = null;
		try {
			educs = qr.query(sql,new BeanListHandler<EduClass>(EduClass.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return educs;
	}
	public static void main(String[] args){
		QueryRunner qr = new QueryRunner(Dhcp.getDataSource());
		EduClassDaoImpl edui = new EduClassDaoImpl(qr);
		EduClass educ = new EduClass();
		educ.setfTeachingClassNumber(103);
		educ.setfClassNumber("101");
		educ.setfTeacherId("144520");
		educ.setfStuNumber(60);
		educ.setfRemark("test");
		int n = edui.save(educ);
		System.out.println("添加了"+n+"行数据");
		
	}
}
